Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./year2.RDS")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.25, n = 442)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 11.40274 11.45561 11.50754 11.55856 11.60870 11.65798 11.70643 11.75407
## [9] 11.80093 11.84704 11.89242 11.93710 11.98110 12.02445 12.06718 12.10925
## [17] 12.15057 12.19113 12.23090 12.26987 12.30801 12.34531 12.38173 12.41726
## [25] 12.45189 12.48557 12.51831 12.55006 12.58082 12.61090 12.64056 12.66971
## [33] 12.69828 12.72618 12.75331 12.77960 12.80495 12.82929 12.85251 12.87455
## [41] 12.89530 12.91469 12.93263 12.95008 12.96788 12.98575 13.00340 13.02054
## [49] 13.03689 13.05217 13.06609 13.07837 13.08872 13.09686 13.10250 13.10576
## [57] 13.10702 13.10641 13.10407 13.10012 13.09469 13.08791 13.07991 13.07081
## [65] 13.06074 13.04984 13.03823 13.02604 13.01340 13.00043 12.98451 12.96347
## [73] 12.93816 12.90942 12.87811 12.84505 12.81111 12.77712 12.74394 12.71240
## [81] 12.68335 12.65764 12.63611 12.61555 12.59250 12.56742 12.54079 12.51306
## [89] 12.48470 12.45618 12.42796 12.40052 12.37431 12.34981 12.32747 12.30777
## [97] 12.29117 12.27602 12.26058 12.24511 12.22990 12.21522 12.20137 12.18862
## [105] 12.17725 12.16755 12.16005 12.15492 12.15186 12.15054 12.15067 12.15194
## [113] 12.15404 12.15666 12.15951 12.16226 12.16463 12.16629 12.16694 12.16628
## [121] 12.16554 12.16603 12.16758 12.17003 12.17320 12.17693 12.18104 12.18537
## [129] 12.18974 12.19400 12.19796 12.20145 12.20432 12.20638 12.20745 12.20753
## [137] 12.20682 12.20548 12.20369 12.20162 12.19945 12.19735 12.19550 12.19406
## [145] 12.19322 12.19315 12.19403 12.19602 12.19930 12.20405 12.21044 12.21864
## [153] 12.22883 12.24182 12.25795 12.27676 12.29773 12.32039 12.34425 12.36882
## [161] 12.39360 12.41811 12.44186 12.46437 12.48513 12.50367 12.51948 12.53572
## [169] 12.55555 12.57852 12.60422 12.63220 12.66203 12.69327 12.72550 12.75827
## [177] 12.79115 12.82371 12.85552 12.88613 12.91511 12.94204 12.96647 12.98797
## [185] 13.00611 13.02044 13.03055 13.03599 13.03814 13.03873 13.03779 13.03536
## [193] 13.03148 13.02620 13.01957 13.01162 13.00239 12.99194 12.98029 12.96751
## [201] 12.95362 12.93867 12.91951 12.89358 12.86183 12.82517 12.78454 12.74087
## [209] 12.69510 12.64816 12.60098 12.55449 12.50962 12.46731 12.42848 12.39408
## [217] 12.35731 12.31234 12.26152 12.20719 12.15172 12.09744 12.04672 12.00189
## [225] 11.96533 11.93245 11.89748 11.86091 11.82324 11.78496 11.74659 11.70862
## [233] 11.67155 11.63588 11.60210 11.57073 11.54225 11.51716 11.49598 11.47720
## [241] 11.45910 11.44178 11.42538 11.41003 11.39584 11.38293 11.37144 11.36149
## [249] 11.35320 11.34669 11.34209 11.33952 11.33910 11.34259 11.35118 11.36414
## [257] 11.38077 11.40036 11.42219 11.44556 11.46974 11.49404 11.51773 11.54011
## [265] 11.56046 11.57807 11.59223 11.60641 11.62412 11.64470 11.66751 11.69189
## [273] 11.71719 11.74275 11.76793 11.79208 11.81453 11.83465 11.85177 11.86748
## [281] 11.88374 11.90045 11.91754 11.93491 11.95247 11.97015 11.98785 12.00548
## [289] 12.02297 12.04022 12.05714 12.07365 12.08966 12.10490 12.11926 12.13286
## [297] 12.14580 12.15822 12.17023 12.18195 12.19350 12.20501 12.21660 12.22837
## [305] 12.24046 12.25298 12.26606 12.27959 12.29336 12.30732 12.32144 12.33566
## [313] 12.34994 12.36424 12.37852 12.39273 12.40683 12.42077 12.43451 12.44800
## [321] 12.46121 12.47408 12.48658 12.49965 12.51409 12.52962 12.54596 12.56283
## [329] 12.57996 12.59705 12.61384 12.63005 12.64539 12.65958 12.67235 12.68341
## [337] 12.69288 12.70113 12.70833 12.71461 12.72014 12.72506 12.72952 12.73368
## [345] 12.73768 12.74168 12.74582 12.75027 12.75516 12.76018 12.76491 12.76935
## [353] 12.77350 12.77737 12.78097 12.78428 12.78733 12.79011 12.79262 12.79488
## [361] 12.79688 12.79862 12.80012 12.80134 12.80228 12.80294 12.80333 12.80345
## [369] 12.80331 12.80292 12.80229 12.80142 12.80032 12.79899 12.79745 12.79571
## [377] 12.79375 12.78921 12.78051 12.76891 12.75566 12.74203 12.72927 12.71863
## [385] 12.71136 12.70874 12.70903 12.70966 12.71063 12.71193 12.71353 12.71543
## [393] 12.71761 12.72006 12.72277 12.72571 12.72889 12.73228 12.73587 12.73965
## [401] 12.74376 12.74833 12.75331 12.75863 12.76425 12.77011 12.77615 12.78233
## [409] 12.78858 12.79486 12.80110 12.80726 12.81328 12.81910 12.82485 12.83070
## [417] 12.83666 12.84272 12.84891 12.85522 12.86167 12.86826 12.87500 12.88190
## [425] 12.88896 12.89620 12.90362 12.91122 12.91897 12.92681 12.93476 12.94280
## [433] 12.95095 12.95921 12.96758 12.97607 12.98467 12.99340 13.00225 13.01123
## [441] 13.02034 13.02959
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.25, n = 442)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 10.83104 10.91083 10.98928 11.06634 11.14199 11.21621 11.28897 11.36024
## [9] 11.42999 11.49821 11.56486 11.62992 11.69335 11.75514 11.81526 11.87374
## [17] 11.93067 11.98605 12.03992 12.09230 12.14321 12.19267 12.24071 12.28735
## [25] 12.33262 12.37652 12.41910 12.46037 12.50035 12.53873 12.57524 12.60995
## [33] 12.64297 12.67437 12.70423 12.73266 12.75972 12.78551 12.81012 12.83362
## [41] 12.85611 12.87767 12.89839 12.91701 12.93250 12.94524 12.95561 12.96401
## [49] 12.97082 12.97643 12.98123 12.98561 12.98996 12.99466 13.00010 13.00508
## [57] 13.00823 13.00971 13.00967 13.00828 13.00569 13.00206 12.99755 12.99230
## [65] 12.98648 12.98024 12.97375 12.96716 12.96062 12.95429 12.94763 12.94004
## [73] 12.93159 12.92233 12.91235 12.90170 12.89046 12.87869 12.86646 12.85384
## [81] 12.84089 12.82769 12.81430 12.80014 12.78467 12.76803 12.75035 12.73177
## [89] 12.71241 12.69243 12.67194 12.65109 12.63001 12.60883 12.58770 12.56673
## [97] 12.54608 12.52171 12.49081 12.45531 12.41715 12.37826 12.34057 12.30602
## [105] 12.27653 12.25406 12.23408 12.21119 12.18602 12.15917 12.13127 12.10292
## [113] 12.07474 12.04734 12.02135 11.99736 11.97601 11.95790 11.94365 11.93388
## [121] 11.92700 11.92104 11.91602 11.91194 11.90881 11.90663 11.90543 11.90519
## [129] 11.90594 11.90769 11.91043 11.91418 11.91895 11.92475 11.93272 11.94385
## [137] 11.95784 11.97443 11.99332 12.01424 12.03692 12.06107 12.08642 12.11268
## [145] 12.13958 12.16684 12.19417 12.22131 12.24797 12.27387 12.29874 12.32229
## [153] 12.34425 12.36797 12.39646 12.42892 12.46455 12.50255 12.54211 12.58243
## [161] 12.62272 12.66217 12.69999 12.73535 12.76748 12.79557 12.81880 12.83990
## [169] 12.86198 12.88484 12.90831 12.93221 12.95634 12.98052 13.00456 13.02829
## [177] 13.05151 13.07405 13.09570 13.11630 13.13566 13.15358 13.16989 13.18439
## [185] 13.19691 13.20727 13.21526 13.22071 13.22509 13.22979 13.23454 13.23908
## [193] 13.24315 13.24647 13.24877 13.24979 13.24926 13.24692 13.24248 13.23570
## [201] 13.22629 13.21399 13.19732 13.17542 13.14896 13.11864 13.08512 13.04908
## [209] 13.01120 12.97217 12.93265 12.89333 12.85489 12.81800 12.78334 12.75160
## [217] 12.71764 12.67708 12.63170 12.58327 12.53358 12.48441 12.43754 12.39474
## [225] 12.35779 12.32231 12.28316 12.24101 12.19651 12.15032 12.10311 12.05553
## [233] 12.00826 11.96194 11.91724 11.87483 11.83535 11.79948 11.76788 11.73781
## [241] 11.70641 11.67411 11.64134 11.60855 11.57617 11.54464 11.51440 11.48588
## [249] 11.45952 11.43576 11.41504 11.39779 11.38445 11.37511 11.36932 11.36670
## [257] 11.36686 11.36944 11.37405 11.38031 11.38784 11.39627 11.40521 11.41429
## [265] 11.42313 11.43135 11.43857 11.44707 11.45903 11.47392 11.49120 11.51034
## [273] 11.53080 11.55206 11.57356 11.59479 11.61520 11.63425 11.65143 11.66894
## [281] 11.68911 11.71157 11.73593 11.76182 11.78886 11.81666 11.84486 11.87306
## [289] 11.90090 11.92799 11.95396 11.97842 12.00099 12.02352 12.04785 12.07366
## [297] 12.10063 12.12843 12.15672 12.18520 12.21352 12.24136 12.26839 12.29429
## [305] 12.31874 12.34139 12.36194 12.38112 12.39992 12.41834 12.43640 12.45411
## [313] 12.47146 12.48848 12.50517 12.52153 12.53759 12.55334 12.56880 12.58397
## [321] 12.59887 12.61350 12.62787 12.64193 12.65562 12.66896 12.68198 12.69469
## [329] 12.70712 12.71929 12.73121 12.74292 12.75443 12.76576 12.77694 12.78798
## [337] 12.79824 12.80719 12.81501 12.82191 12.82806 12.83366 12.83890 12.84396
## [345] 12.84904 12.85432 12.86000 12.86626 12.87330 12.88083 12.88842 12.89605
## [353] 12.90369 12.91131 12.91888 12.92637 12.93376 12.94102 12.94811 12.95501
## [361] 12.96170 12.96814 12.97430 12.98023 12.98598 12.99156 12.99698 13.00225
## [369] 13.00737 13.01236 13.01721 13.02194 13.02656 13.03106 13.03546 13.03977
## [377] 13.04400 13.04775 13.05078 13.05328 13.05546 13.05751 13.05964 13.06204
## [385] 13.06491 13.06846 13.07267 13.07733 13.08234 13.08761 13.09301 13.09846
## [393] 13.10384 13.10906 13.11401 13.11860 13.12270 13.12623 13.12908 13.13115
## [401] 13.13274 13.13423 13.13560 13.13683 13.13789 13.13877 13.13945 13.13991
## [409] 13.14012 13.14008 13.13975 13.13911 13.13816 13.13686 13.13530 13.13354
## [417] 13.13159 13.12943 13.12707 13.12449 13.12168 13.11866 13.11539 13.11189
## [425] 13.10815 13.10415 13.09990 13.09538 13.09059 13.08550 13.08013 13.07448
## [433] 13.06856 13.06237 13.05591 13.04920 13.04224 13.03502 13.02757 13.01987
## [441] 13.01195 13.00379
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.25, n = 442)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 10.86332 10.92271 10.98110 11.03848 11.09487 11.15024 11.20461 11.25795
## [9] 11.31028 11.36158 11.41185 11.46109 11.50929 11.55645 11.60256 11.64766
## [17] 11.69178 11.73491 11.77705 11.81820 11.85834 11.89747 11.93560 11.97270
## [25] 12.00878 12.04383 12.07785 12.11083 12.14276 12.17354 12.20309 12.23144
## [33] 12.25863 12.28470 12.30969 12.33363 12.35658 12.37856 12.39961 12.41977
## [41] 12.43908 12.45759 12.47532 12.49213 12.50787 12.52259 12.53631 12.54906
## [49] 12.56090 12.57184 12.58192 12.59119 12.59967 12.60740 12.61441 12.62064
## [57] 12.62599 12.63047 12.63406 12.63678 12.63861 12.63957 12.63964 12.63882
## [65] 12.63713 12.63454 12.63107 12.62671 12.62146 12.61533 12.60682 12.59483
## [73] 12.57989 12.56256 12.54337 12.52287 12.50161 12.48012 12.45896 12.43866
## [81] 12.41977 12.40283 12.38839 12.37424 12.35803 12.34013 12.32089 12.30064
## [89] 12.27976 12.25859 12.23749 12.21680 12.19688 12.17807 12.16075 12.14525
## [97] 12.13192 12.11993 12.10818 12.09667 12.08538 12.07433 12.06348 12.05285
## [105] 12.04242 12.03219 12.02286 12.01499 12.00835 12.00272 11.99787 11.99355
## [113] 11.98955 11.98564 11.98158 11.97715 11.97211 11.96624 11.95931 11.95108
## [121] 11.94162 11.93126 11.92019 11.90861 11.89671 11.88467 11.87268 11.86095
## [129] 11.84965 11.83898 11.82913 11.82029 11.81266 11.80641 11.79993 11.79166
## [137] 11.78191 11.77096 11.75914 11.74673 11.73403 11.72137 11.70902 11.69730
## [145] 11.68651 11.67694 11.66891 11.66271 11.65864 11.65701 11.65812 11.66227
## [153] 11.66976 11.68059 11.69433 11.71060 11.72906 11.74933 11.77106 11.79389
## [161] 11.81746 11.84139 11.86535 11.88896 11.91185 11.93369 11.95409 11.97667
## [169] 12.00489 12.03812 12.07576 12.11720 12.16182 12.20902 12.25818 12.30869
## [177] 12.35994 12.41132 12.46222 12.51202 12.56012 12.60591 12.64876 12.68808
## [185] 12.72325 12.75366 12.77870 12.79775 12.81346 12.82873 12.84338 12.85724
## [193] 12.87011 12.88183 12.89219 12.90102 12.90814 12.91337 12.91651 12.91739
## [201] 12.91583 12.91163 12.90211 12.88533 12.86230 12.83399 12.80140 12.76553
## [209] 12.72736 12.68788 12.64809 12.60898 12.57153 12.53675 12.50561 12.47911
## [217] 12.44985 12.41148 12.36657 12.31771 12.26747 12.21845 12.17321 12.13435
## [225] 12.10443 12.07858 12.05051 12.02068 11.98955 11.95759 11.92526 11.89304
## [233] 11.86137 11.83073 11.80158 11.77439 11.74962 11.72773 11.70919 11.69386
## [241] 11.68109 11.67060 11.66211 11.65531 11.64993 11.64567 11.64224 11.63936
## [249] 11.63674 11.63409 11.63112 11.62755 11.62307 11.61866 11.61540 11.61322
## [257] 11.61206 11.61184 11.61250 11.61397 11.61618 11.61905 11.62252 11.62652
## [265] 11.63097 11.63582 11.64099 11.64735 11.65565 11.66555 11.67675 11.68894
## [273] 11.70180 11.71501 11.72827 11.74126 11.75366 11.76517 11.77547 11.78528
## [281] 11.79551 11.80610 11.81701 11.82819 11.83961 11.85120 11.86292 11.87473
## [289] 11.88658 11.89842 11.91020 11.92189 11.93342 11.94589 11.96020 11.97607
## [297] 11.99321 12.01132 12.03013 12.04934 12.06867 12.08782 12.10650 12.12444
## [305] 12.14133 12.15690 12.17085 12.18418 12.19800 12.21220 12.22664 12.24120
## [313] 12.25578 12.27023 12.28446 12.29832 12.31171 12.32449 12.33656 12.34778
## [321] 12.35805 12.36722 12.37520 12.38198 12.38777 12.39268 12.39683 12.40034
## [329] 12.40334 12.40595 12.40828 12.41047 12.41263 12.41488 12.41735 12.42016
## [337] 12.42300 12.42552 12.42773 12.42968 12.43141 12.43293 12.43430 12.43554
## [345] 12.43668 12.43777 12.43882 12.43989 12.44099 12.44127 12.43999 12.43739
## [353] 12.43371 12.42918 12.42406 12.41856 12.41294 12.40744 12.40228 12.39771
## [361] 12.39398 12.39130 12.38994 12.38905 12.38770 12.38602 12.38411 12.38208
## [369] 12.38003 12.37807 12.37631 12.37487 12.37384 12.37334 12.37347 12.37435
## [377] 12.37608 12.37883 12.38256 12.38705 12.39212 12.39754 12.40311 12.40862
## [385] 12.41387 12.41864 12.42402 12.43103 12.43939 12.44881 12.45902 12.46972
## [393] 12.48063 12.49147 12.50196 12.51181 12.52074 12.52846 12.53469 12.53915
## [401] 12.54275 12.54651 12.55038 12.55428 12.55812 12.56185 12.56538 12.56865
## [409] 12.57157 12.57408 12.57610 12.57755 12.57837 12.57847 12.57812 12.57758
## [417] 12.57683 12.57587 12.57466 12.57318 12.57142 12.56935 12.56696 12.56422
## [425] 12.56112 12.55763 12.55373 12.54940 12.54461 12.53935 12.53364 12.52748
## [433] 12.52090 12.51391 12.50653 12.49877 12.49065 12.48218 12.47337 12.46425
## [441] 12.45483 12.44513
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")
keeping in case
#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")